<template>
	<view class="uni-common-mt">

		<view class="title">
			<image @click="back()" src="../../static/fanhui.png" style="float: left; width: 20px;vertical-align: middle;height: 20px;"
				mode=""></image> 编辑资料

		</view>
		<view class="content">
			<view class="label">
				用户名
			</view>

			<view class="uni-form-item uni-column">
				<input type="text" class="uni-input" disabled v-model="form.username">
			</view>
			<view class="label">
				真实姓名
			</view>

			<view class="uni-form-item uni-column">
				<input type="text" class="uni-input" v-model="form.name">
			</view>
			<view class="label">
				电话
			</view>

			<view class="uni-form-item uni-column">
				<input type="text" class="uni-input" v-model="form.phone">
			</view>
			<view class="label">
				邮箱
			</view>

			<view class="uni-form-item uni-column">
				<input type="text" class="uni-input" v-model="form.email">
			</view>
			<view class="label">
				头像
			</view>
			<view class="uni-form-item uni-column">
				<el-upload class="avatar-uploader" action="http://demonuxtapi.dishait.cn/admin/s/upload"
					:headers="headers" :show-file-list="false" :on-success="handleAvatarSuccess">
					<img v-if="form.avator" :src="form.avator" class="avatar" />
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</view>
			<view class="submit">
				<button @click="submit()">提交</button>
								<button @click="tcdl()">退出登录</button>
			</view>


		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {},
				imageUrl: "",
				headers: {
					token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6IjQ1NioqKkBxcS5jb20iLCJwaG9uZSI6IjEzNyoqKio1NDQ5IiwiaWQiOjEsInVzZXJuYW1lIjoiY2VzaGkxIiwibmlja25hbWUiOiIiLCJwYXNzd29yZCI6IjA5MTNmYWIyOWY4YWE5MmY5N2ZlNjQ2ODM5NDA5ZjM3MjZjNGQzZGM2NGY0NDlmMTA2MjJhNTUwMGM3NDBjMDIiLCJhdmF0YXIiOiIiLCJ3ZWl4aW5fdW5pb25pZCI6bnVsbCwic2V4Ijoi5pyq55-lIiwiZGVzYyI6IiIsInN0YXR1cyI6MSwiY3JlYXRlZF90aW1lIjoiMjAyMS0wMi0yMVQxNDowNDowMi4wMDBaIiwidXBkYXRlZF90aW1lIjoiMjAyMy0xMC0wNFQxNjo0ODo0MC4wMDBaIiwiaWF0IjoxNjk4NTYyOTU4fQ.MLAxkr9JkR9lIxEZbufSuBDB_kz-GR8UYEvwB3NslcA",
					schoolid: "11"

				},
			}
		},
		mounted() {
			this.form = JSON.parse(window.localStorage.getItem("userinfo"));
			console.log(this.form)
		},
		methods: {
			tcdl(){
				uni.navigateTo({
					url:"/pages/index/index"
				})
			},
			back(){
			uni.switchTab({
				url:"/pages/my/my"
			});
			},
			isEmail(email) {
			  let emailRegExp = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
			  var result=emailRegExp.test(email)
			  if(!result){
				  uni.showToast({
				  	icon:"none",
				  	title: "邮箱格式不正确"
				  })
				  return false
			  }else{
				 return true 
			  }
			
			},

			checkmobile(mobile) {
				var re = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
				var result = re.test(mobile);
				if (!result) {
					uni.showToast({
						icon:"none",
						title: "手机号格式不正确"
					})
					return false
				} else {
					return true
				}
			},
			submit() {
				if (!this.checkmobile(this.form.phone)) {
					uni.showToast({
							icon:"none",
						title: "请输入正确的手机号"
					})
				} else if(!this.isEmail(this.form.email)){
					uni.showToast({
							icon:"none",
						title: "请输入正确的邮箱"
					})
				}
				
				else {
					uni.request({
						url: "/index/adduser",
						method: "POST",
						data: this.form,
						success(res) {
							console.log(res)
							console.log(this.form)
							uni.showToast({
								title:"完善资料成功,请重新登录",
								icon:"success"
								
							})
						
							uni.navigateTo({
								url:"/pages/index/index"
							});
						}
					})
				}

			},
			handleAvatarSuccess(e) {
				console.log(e)
				this.form.avator = e.data
			},
			sctx() {
				uni.chooseImage({
					success(res) {
						console.log(res)
						uni.request({
							url: "admin/s/upload",
							method: "POST",
							headers: {
								token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6IjQ1NioqKkBxcS5jb20iLCJwaG9uZSI6IjEzNyoqKio1NDQ5IiwiaWQiOjEsInVzZXJuYW1lIjoiY2VzaGkxIiwibmlja25hbWUiOiIiLCJwYXNzd29yZCI6IjA5MTNmYWIyOWY4YWE5MmY5N2ZlNjQ2ODM5NDA5ZjM3MjZjNGQzZGM2NGY0NDlmMTA2MjJhNTUwMGM3NDBjMDIiLCJhdmF0YXIiOiIiLCJ3ZWl4aW5fdW5pb25pZCI6bnVsbCwic2V4Ijoi5pyq55-lIiwiZGVzYyI6IiIsInN0YXR1cyI6MSwiY3JlYXRlZF90aW1lIjoiMjAyMS0wMi0yMVQxNDowNDowMi4wMDBaIiwidXBkYXRlZF90aW1lIjoiMjAyMy0xMC0wNFQxNjo0ODo0MC4wMDBaIiwiaWF0IjoxNjk4NTYyOTU4fQ.MLAxkr9JkR9lIxEZbufSuBDB_kz-GR8UYEvwB3NslcA",
								schoolid: "11"

							},
							data: {
								// file:res.tempFilePaths[0]
								// file: `‪C:\Users\NINGMEI\Pictures\1.jpeg`
							},
							success: (res1) => {
								console.log(res1)
							}


						})

					}
				})
			}

		}
	}
</script>
<style>
	.el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
<style scoped>
	.uni-common-mt {
		width: 100%;
		height: 100%;
		background: #e5e5e5;

	}

	.uni-form-item {
		margin-top: 10px;
		margin-bottom: 10px;

	}

	.content {

		padding: 10px;
		box-sizing: border-box;
	}

	.label {
		font-size: 14px;

	}

	.uni-input {
		background: #fff;
		height: 30px;
		line-height: 30px;
		padding: 0px 10px;
		box-sizing: border-box;
		border-radius: 2px;
	}

	.title {
		width: 100%;
		height: auto;
		padding: 10px;
		box-sizing: border-box;
		background: purple;
		color: #fff;
		text-align: center;
		font-size: 14px;
	}
</style>